home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / docs / misc / ConcNews.lha / news / amiga.compilers / comp.sys.amiga.programmer_33908_000036.msg < prev    next >
Encoding:
Text File  |  1994-11-27  |  2.7 KB  |  92 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: news.chalmers.se!sunic!EU.net!howland.reston.ans.net!gatech!concert!sas!mozart.unx.sas.com!walker
  3. From: walker@twix.unx.sas.com (Doug Walker)
  4. Subject: Re: Allocating Mem & Loading
  5. Originator: walker@twix.unx.sas.com
  6. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  7. Message-ID: <CrC6F6.KM3@unx.sas.com>
  8. Date: Mon, 13 Jun 1994 12:52:17 GMT
  9. References:  <2tdfm9$pvd@dsm6.dsmnet.com>
  10. Nntp-Posting-Host: twix.unx.sas.com
  11. Organization: SAS Institute Inc.
  12. Lines: 79
  13.  
  14.  
  15. In article <2tdfm9$pvd@dsm6.dsmnet.com>, mikebockert@dsm1.dsmnet.com writes:
  16. |> I would like to allocate memory for an array of a structure.  The number of the
  17. |> array will be deterimined by my data file, so it is not the same everytime. 
  18. |> Well the question is how can I load the entire file into this array without
  19. |> having a loop reading in one set of the array at a time.  I want to do one read
  20. |> statement to load the entire file into the array of strutures.. 
  21. |>  
  22. |> Example:
  23. |>  
  24. |> long bytes,value;
  25. |>  
  26. |> bytes = FileSize(file);
  27. |> value = bytes / sizeof struct(A);
  28. |>  
  29. |> - then allocate the memory for however many array's we need (Value) and then
  30. |>   I want to do a single read command to load the entire file into that array
  31. |>  
  32. |> Does anyone know how I could do this?  thanks
  33.  
  34. This is somewhat SAS/C dependent, but it might work with the new
  35. commercial DICE as well (I think it has the __aligned keyword):
  36.  
  37.    #include <proto/dos.h>
  38.    #include <stdio.h>
  39.  
  40.    int FileSize(char *filename)
  41.    {
  42.       __aligned struct FileInfoBlock fib;
  43.       BPTR fh;
  44.       int size = -1;
  45.    
  46.       if(fh = Lock(filename, SHARED_LOCK))
  47.       {
  48.          if(Examine(fh, &fib) == DOSTRUE)
  49.          {
  50.             size = fib.fib_Size;
  51.          }
  52.          UnLock(fh);
  53.       }
  54.       return size;
  55.    }
  56.  
  57. The following version should work with any C compiler:
  58.  
  59.    #include <clib/dos_protos.h>
  60.    #include <stdio.h>
  61.    
  62.    int FileSize(char *filename)
  63.    {
  64.       struct FileInfoBlock *fib;
  65.       BPTR fh;
  66.       int size = -1;
  67.       
  68.       fib = malloc(sizeof(struct FileInfoBlock));
  69.       if(fib == NULL) return size;
  70.       
  71.       if(fh = Lock(filename, SHARED_LOCK))
  72.       {
  73.          if(Examine, fh, fib) == DOSTRUE)
  74.          {
  75.             size = fib->fib_Size;
  76.          }
  77.          UnLock(fh);
  78.       }
  79.       return size;
  80.    }
  81.  
  82. Your single read command is an fread().  Just allocate a buffer the
  83. size you need (using malloc()) and then fread() into the buffer.
  84.  
  85. -- 
  86.   *****                    / walker@unx.sas.com
  87.  *|_o_o|\\     Doug Walker<  BIX, Portal: djwalker
  88.  *|. o.| ||                \ CompuServe: 71165,2274
  89.   | o  |//     
  90.   ====== 
  91. Any opinions expressed are mine, not those of SAS Institute, Inc.
  92.